<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('order.info.user_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.user_id"
                        prop="user_id"
                        :input-attr="{ pk: 'ba_user.nickname', field: 'nickname', remoteUrl: '/admin/user.User/index', onRow: GetUserInfo }"
                        :placeholder="t('Please select field', { field: t('order.info.user_id') })"
                    />
                    <FormItem
                        ref="carInfoRef"
                        :label="t('order.info.car_info_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.car_info_id"
                        prop="car_info_id"
                        :input-attr="{
                            pk: 'ba_car_info.car_code',
                            field: 'car_code',
                            remoteUrl: '/admin/car.Info/index?user_id=' + baTable.form.items!.u_id,
                            onBlur: CancelCarInfo,
                            tooltipParams: {
                                联系人昵称: 'car_code',
                                交易地址: 'car_model',
                                联系人电话: 'car_model',
                            },
                            labelFormatter: (row: any) => {
                                return row.car_code + ' ' + row.car_model
                            },
                        }"
                        :placeholder="t('Please select field', { field: t('order.info.car_info_id') })"
                    />
                    <FormItem
                        :label="t('order.info.order_date')"
                        type="date"
                        v-model="baTable.form.items!.order_date"
                        prop="order_date"
                        :placeholder="t('Please select field', { field: t('order.info.order_date') })"
                    />
                    <FormItem
                        :label="t('order.info.pay_date')"
                        type="date"
                        v-model="baTable.form.items!.pay_date"
                        prop="pay_date"
                        :placeholder="t('Please select field', { field: t('order.info.pay_date') })"
                    />
                    <FormItem
                        :label="t('order.info.order_type_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.order_type_id"
                        prop="order_type_id"
                        :input-attr="{ pk: 'ba_order_type.id', field: 'order_type', remoteUrl: '/admin/order.Type/index' }"
                        :placeholder="t('Please select field', { field: t('order.info.order_type_id') })"
                    />
                    <FormItem
                        :label="t('order.info.purpose_info_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.purpose_info_id"
                        prop="purpose_info_id"
                        :input-attr="{ pk: 'ba_purpose_info.id', field: 'purpose_name', remoteUrl: '/admin/purpose.Info/index' }"
                        :placeholder="t('Please select field', { field: t('order.info.purpose_info_id') })"
                    />
                    <FormItem
                        :label="t('order.info.insurance_info_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.insurance_info_id"
                        prop="insurance_info_id"
                        :input-attr="{ pk: 'ba_insurance_info.id', field: 'insurance_name', remoteUrl: '/admin/insurance.Info/index' }"
                        :placeholder="t('Please select field', { field: t('order.info.insurance_info_id') })"
                    />
                    <FormItem
                        :label="t('order.info.insurance_salesman_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.insurance_salesman_id"
                        prop="insurance_salesman_id"
                        :input-attr="{ pk: 'ba_insurance_salesman.id', field: 'name', remoteUrl: '/admin/insurance.Salesman/index' }"
                        :placeholder="t('Please select field', { field: t('order.info.insurance_salesman_id') })"
                    />
                    <FormItem
                        :label="t('order.info.remark')"
                        type="textarea"
                        v-model="baTable.form.items!.remark"
                        prop="remark"
                        :input-attr="{ rows: 3 }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('order.info.remark') })"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
// 获取车辆选择框的ref
const carInfoRef = ref()

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    order_date: [buildValidatorData({ name: 'date', title: t('order.info.order_date') })],
    pay_date: [buildValidatorData({ name: 'date', title: t('order.info.pay_date') })],
    update_time: [buildValidatorData({ name: 'date', title: t('order.info.update_time') })],
    create_time: [buildValidatorData({ name: 'date', title: t('order.info.create_time') })],
})

const CancelCarInfo = () => {
    console.log('触发失去焦点')
    if (baTable.form.items!.car_info_id == ' ') {
        baTable.form.items!.car_info_id = baTable.form.items!.car_info_id + ' '
    }
}
const GetUserInfo = (row: any) => {
    baTable.form.items!.u_id = row.id
    baTable.form.items!.car_info_id = ' '
    console.log('触发人员选择')
}
</script>

<style scoped lang="scss"></style>
